<?php
/*
 * @author Carlos Avila
 */


class Videos extends ActiveRecord
{  
    	public $debug = FALSE;        
        const INACTIVO  =   0;
        const ACTIVO    =   1;
        const ELIMINADO =   2;
        
    public function getVideo($video_id)
    {
        return $this->find_first("conditions: id = $video_id and estatus = ".Videos::ACTIVO."|| estatus = ".Videos::INACTIVO);        
    }
    
    public function getAllVideos($page, $ppage=20)
    {
        return $this->paginate("page: $page", "per_page: $ppage", 'conditions: estatus = '.Videos::ACTIVO.' || estatus = '.Videos::INACTIVO, 'order: id desc');
    }
    
    
    public function getVideos($page, $ppage=20)
    {
        return $this->paginate("page: $page", "per_page: $ppage", 'conditions: estatus='.Videos::ACTIVO, 'order: id desc');
    }        
    
    public function getAllVideosbyVisits($page, $ppage=20)
    {
        return $this->paginate("page: $page", "per_page: $ppage", 'conditions: estatus='.Videos::ACTIVO, 'order: visitas desc');        
    }
    
    public function getVideosByVisitsByCategoria($categoria_name="",$page=1, $ppage=20)
    {        
        $sql = "select videos.* from videos 
        inner join subcategorias on subcategorias.id = videos.subcategorias_id
        inner join categorias on categorias.id= subcategorias.categorias_id and categorias.nombre = '$categoria_name'
        where videos.estatus = ".Videos::ACTIVO;          
        return $this->paginate_by_sql($sql, "per_page: $ppage", "page: $page");        
    }
    
    public function getVideosByVisitsBySubcategoria($subcategoria_name="",$page=1, $ppage=20)
    {        
        $sql = "select videos.* from videos 
        inner join subcategorias on subcategorias.id = videos.subcategorias_id and subcategorias.nombre = '$subcategoria_name'
        inner join categorias on categorias.id= subcategorias.categorias_id 
        where videos.estatus = ".Videos::ACTIVO;          
        return $this->paginate_by_sql($sql, "per_page: $ppage", "page: $page");        
    }
    
    public function getLast()
    {
        return $this->find('conditions: estatus='.Videos::ACTIVO, 'order: fecha_at desc limit 5');        
    }
   
    public function buscar($video_id)
    {
        return $this->find("conditions: id = $video_id and estatus = ".Videos::ACTIVO);
    }        
    
    public function actualizar($video) 
    {
        return $this->update($video);                            
    }
    
    public function getByCategorias($categoria_id,$subcategorias_id = "subcategorias.id") 
    {
        $sql = "select * from videos 
        inner join subcategorias on subcategorias.id = videos.subcategorias_id
        inner join categorias on $categoria_id = subcategorias.categorias_id
        where videos.estatus = ".Videos::ACTIVO." group by videos.id";        
        
        return $this->paginate_by_sql($sql);        
    }
    
    public function getBySubcategoria($subcategoria_id) 
    {        
        return $this->find(" conditions : subcategorias_id = $subcategoria_id and estatus = ".Videos::ACTIVO);
    }
    
    public function getVideosByCategoriaSubcategoria($categoria_name='', $subcategoria_name='')
    {        
        $categoria_id = Load::model('categorias')->getIdByName($categoria_name);
        $subcategoria_id = Load::model('subcategorias')->getIdByName($subcategoria_name);
                
        if($subcategoria_id != '')
        return $this->getByCategorias($categoria_id,$subcategoria_id);                                                
        return $this->getByCategorias($categoria_id);
    }
    
    public function getMoreVisited($limit=10)
    {                   
        return $this->find("order: visitas desc limit $limit"," conditions: estatus = ".Videos::ACTIVO);
    }    
    
    public function setVisitas() 
    {
        $this->visitas++;
        return $this->update();                            
    }
    
    public function borrar() 
    {
        $this->estatus = Videos::ELIMINADO;
        return $this->update();                            
    }  
}